<?php
class Product_list extends MY_Front_Controller {

	function __construct() {
		parent::__construct();
		$this->load->helper('MY_url_helper');
		$this->load->model('product_model');
		$this->load->model('category_model');
		$this->load->model('languages_model');
		$this->load->model('lang_general_model');
		
		
		$this->session->set_userdata(array('post' => $this->input->post()));
	
		$segment_array = $this->uri->segment_array();
		
		$language = $this->languages_model->get_by_params(array('key' => $segment_array[1]))->result();
		$language_id = $language[0]->id;
		
		if ($this->session->userdata('product_search') && !$this->input->post()){
			$session_search = $this->session->userdata('product_search');
			
			$previous_language = $session_search['language_id'];
			$prev_language = $this->languages_model->get_by_id($previous_language);

			if ($previous_language != $language_id){
				if ($segment_array[4] != 0){ // if we have filter by category, we search for the category in the new language
					$category_id = explode("-", $segment_array[4]);
					$category_id = $category_id[0];
					
					$category = $this->category_model->get_by_id($category_id);
					
					if ($previous_language == 1){
						$change_to_category = $this->category_model->getAll(array('language_id' => $language_id, 'original_id' => $category->id ))->result();
						if (!empty($change_to_category)){
							$segment_array[4] = $change_to_category[0]->id . '-' . string_to_url($change_to_category[0]->name);
						} else {
							$segment_array[4] = 0;
						}
					} elseif ($language_id == 1){
						$change_to_category = $this->category_model->get($category->original_id);
						if (!empty($change_to_category)){
							$segment_array[4] = $change_to_category->id . '-' . string_to_url($change_to_category->name);
						} else {
							$segment_array[4] = 0;
						}
					} else {
						$change_to_category = $this->category_model->getAll(array('language_id' => $language_id, 'original_id' => $category->original_id ))->result();
						if (!empty($change_to_category)){
							$segment_array[4] = $change_to_category[0]->id . '-' . string_to_url($change_to_category[0]->name);
						} else {
							$segment_array[4] = 0;
						}
					}
				}
						
				if (isset($segment_array[5])){  //if we have filtering
					
					//getting the filters
					$pre_filter = explode(';',html_entity_decode(urldecode($segment_array[5])));

					foreach ($pre_filter as $key=>$value){
						$filter_tags = explode("=",$value);
						if ((isset($filter_tags[1]))) {
							$filter[$filter_tags[0]] = $filter_tags[1];
						}						
					}
					
					//checking for filters
					if (isset($filter['metal'])){
						$filter_lang = $this->lang_general_model->getAll(array('by_value' => array( $prev_language->key, $filter['metal'])))->result();
					
						if (!empty($filter_lang)){
							$filter['metal'] = lang($filter_lang[0]->key);
						} else {
							unset($filter['metal']);
						}
					}
					
					if (isset($filter['gem'])){
						$filter_lang = $this->lang_general_model->getAll(array('by_value' => array( $prev_language->key, $filter['gem'])))->result();
						if (!empty($filter_lang)){
							$filter['gem'] = lang($filter_lang[0]->key);
						} else {
							unset($filter['gem']);
						}
					}
					
					if (isset($filter['shape'])){
						$filter_lang = $this->lang_general_model->getAll(array('by_value' => array( $prev_language->key, $filter['shape'])))->result();
						if (!empty($filter_lang)){
							$filter['shape'] = lang($filter_lang[0]->key);
						} else {
							unset($filter['shape']);
						}
					}
					
					if (isset($filter['setting'])){
						$filter_lang = $this->lang_general_model->getAll(array('by_value' => array( $prev_language->key, $filter['setting'])))->result();
						if (!empty($filter_lang)){
							$filter['setting'] = lang($filter_lang[0]->key);
						} else {
							unset($filter['setting']);
						}
					}
					
					//rebuild filter segment
					
					if (!empty($filter)){
						$segment_array[5] = '';
						foreach ($filter as $key=>$value){
							$segment_array[5] .= $key . "=" . urlencode($value) . ';';
						}
						
						$segment_array[5] = substr($segment_array[5], 0, -1);
					} else {
						unset($segment_array[5]);
					}
				}
			}
			
			$this->session->unset_userdata('product_search');

			redirect(base_url(join('/', $segment_array)));
		}
	}
	
	public function index(){
	}
	
	public function get_list($count = 0, $cat_id = 0, $filter_string = ''){
		$this->db->cache_on();
		
		$filter = array();
		$pre_filter = array();
		if (strlen($filter_string)){
			$pre_filter = explode(';',html_entity_decode(urldecode($filter_string)));

			foreach ($pre_filter as $key=>$value){
				$filter_tags = explode("=",$value);
				if ((isset($filter_tags[1]))) {
					$filter[$filter_tags[0]] = $filter_tags[1];
				}
				
			}
	
		}
		$this->load->library('pagination');
		$this->load->library('user_agent');
		
		$search = $this->getFilters($filter,$cat_id);

		$search['language_id'] = $this->config->item('lang_obj')->id;
				
		$search['per_page'] = (isset($search['per_page']) ? $search['per_page'] : $this->config->item('products_per_page'));
	
		$this->session->set_userdata(array('product_search' =>$search));
		$this->session->set_userdata(array('product_search_url' =>current_url()));

		$config['base_url'] = site_url();
		$total = $this->product_model->getProductList($search, true)->result();
		$config['total_rows'] = (int) $total[0]->cnt;
		$config['per_page'] = isset($search['per_page']) ? $search['per_page'] : $this->config->item('products_per_page');
		$config['uri_segment'] = 2;
		$config['num_links'] = 4;
		$config['first_link'] = lang('first_elem');
		$config['last_link'] = lang('last_elem');
		
		$search['to'] = $config['per_page'];
		$search['from'] = $count;	
		$search['total'] = $config['total_rows'];

		$data['products'] = array();
		if ($config['total_rows'] > 0){
			$data['products'] = $this->product_model->getProductList($search)->result();
		}
		/*foreach($child_products as $prod){
				$prod_parent = $this->product_model->getProductList(array('id' => $prod->parent_id))->result();
			if ($this->config->item('lang_obj')->id == $this->config->item('def_language_id')){
				$data['products'][] = $prod_parent[0];
			} else {
				$prod_parent = $this->product_model->getProductList(array('sku' => $prod_parent->sku, 'language_id' => $prod->language_id))->result();
				$data['products'][] = $prod_parent[0];
			}
		}*/
			
		if ($cat_id){
			$category = $this->category_model->get($cat_id);
			$title = $category->name;
			
			while ($category->parent_id){
				$category = $this->category_model->get($category->parent_id);
			}
			if ($this->config->item('lang_obj')->id != $this->config->item('def_language_id')){
				$data['styles'] = $this->category_model->getAll(array('language_id'=>$this->config->item('lang_obj')->id, 'original_id' =>$category->id))->result();
			} else {
				$data['styles'] = $this->category_model->getAll(array('language_id'=>$this->config->item('lang_obj')->id, 'parent_id' =>$category->id))->result();
			}
		}
	
		$this->pagination->initialize($config);
		
		$data['links'] = $this->pagination->create_links();
	
		$data['search'] = $search;
		$data['category_id'] = $cat_id;
		
		$this->template->add_js('application/assets/js/product_list.js');
		
		if (isset($title)){
			$this->template->write('pageTitle', $title);
		}
		$this->template->write_view ( 'content', 'front/list_view.php', $data );
	  	$this->template->render ();
	  	$this->db->cache_off();
	 
	}
	
	public function get_url(){
		$post = $this->input->post();

		$current_url = $post['current_url'];
	
		$segment_array = explode('/',$current_url);

		$filter_name = $post['type'];
		$new_value = $post['value'];

		if (isset($segment_array[7]) && $segment_array[4] == 'product_list'){
			if (strpos($segment_array[7], $filter_name) !== FALSE){
				$segment_element = explode(";",$segment_array[7]);
				
				foreach ($segment_element as $idx=>$element){
					if (strpos($element, $filter_name) !== FALSE){
						if ($element == $filter_name . '=' . $new_value){
							unset($segment_element[$idx]);
						} else {
							$segment_element[$idx] = substr($element, 0, strpos($element, "="));
							$segment_element[$idx] .= '=' .$new_value;
						}
					}
				}
				
				$segment_array[7] = join(';', $segment_element);
				
			} else {
				$segment_array[7] .= ';' . $filter_name . "=" . $new_value;
			}
		} else {

			if(!isset($segment_array[4]) OR $segment_array[4] != 'product_list'){
				$newArray = array();
				$newArray[] = $segment_array[0];
				$newArray[] = $segment_array[1];
				$newArray[] = $segment_array[2];
				$newArray[] = $segment_array[3];
				$newArray[4] = 'product_list';
				$newArray[] = 0;
				$newArray[] = 0;
				
				$segment_array = $newArray;
			}
			$segment_array[] = $filter_name . "=" . $new_value;
		}
			
		echo json_encode(array('status' => 1, 'url' => join('/',$segment_array)));
	}
}